package com.ruoyi.uni.service.impl;

import java.util.List;

import com.ruoyi.common.utils.MD5;

import com.ruoyi.uni.domain.DcGoods;
import com.ruoyi.uni.mapper.DcGoodsMapper;
import com.ruoyi.uni.service.IDcGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * 餐品管理Service业务层处理
 * 
 * @author hx
 * @date 2024-04-17
 */
@Service
public class DcGoodsServiceImpl implements IDcGoodsService
{
    @Autowired
    private DcGoodsMapper dcGoodsMapper;

    /**
     * 查询餐品管理
     * 
     * @param id 餐品管理主键
     * @return 餐品管理
     */
    @Override
    public DcGoods selectDcGoodsById(String id)
    {
        return dcGoodsMapper.selectDcGoodsById(id);
    }

    @Override
    @Transactional
    public String move(String id, int flag, Long type) {
        String ret="";
        DcGoods Myself  = dcGoodsMapper.selectDcGoodsById(id);
        if(Myself==null)
            return "数据错误";
        switch(flag){
            case 1:
                DcGoods Up = dcGoodsMapper.selectMoveUp(Myself.getLocation(),type);
                if(Up==null)
                    return "已经处于最上，无法上移&只能在同类型中移动";
                dcGoodsMapper.updateLocationById(Myself.getId(), Up.getLocation());
                dcGoodsMapper.updateLocationById(Up.getId(), Myself.getLocation());
                ret= "上移成功";
                break;
            case 2:
                DcGoods Down = dcGoodsMapper.selectMoveDown(Myself.getLocation(),type);
                if(Down==null)
                    return "已经处于最下，无法下移&只能在同类型中移动";
                dcGoodsMapper.updateLocationById(Myself.getId(), Down.getLocation());
                dcGoodsMapper.updateLocationById(Down.getId(), Myself.getLocation());
                ret="下移成功";
                break;
        }
        return ret;
    }

    /**
     * 查询餐品管理列表
     * 
     * @param dcGoods 餐品管理
     * @return 餐品管理
     */
    @Override
    public List<DcGoods> selectDcGoodsList(DcGoods dcGoods)
    {
        return dcGoodsMapper.selectDcGoodsList(dcGoods);
    }

    /**
     * 新增餐品管理
     * 
     * @param dcGoods 餐品管理
     * @return 结果
     */
    @Override
    public int insertDcGoods(DcGoods dcGoods)
    {
        System.out.println("dcGoods"+dcGoods);
        dcGoods.setId(MD5.MD5_16("xx"));
        dcGoods.setLocation(dcGoodsMapper.selectDcGoodsCount(dcGoods.getType())+1);
        dcGoods.setStatus(1);
        System.out.println(dcGoods);
        return dcGoodsMapper.insertDcGoods(dcGoods);
    }

    /**
     * 修改餐品管理
     * 
     * @param dcGoods 餐品管理
     * @return 结果
     */
    @Override
    public int updateDcGoods(DcGoods dcGoods)
    {
        Long l = dcGoodsMapper.selectDcGoodsCount(dcGoods.getType());
        dcGoods.setLocation(l+1);
        return dcGoodsMapper.updateDcGoods(dcGoods);
    }

    /**
     * 批量删除餐品管理
     * 
     * @param ids 需要删除的餐品管理主键
     * @return 结果
     */
    @Override
    public int deleteDcGoodsByIds(String[] ids)
    {
        return dcGoodsMapper.deleteDcGoodsByIds(ids);
    }

    /**
     * 删除餐品管理信息
     * 
     * @param id 餐品管理主键
     * @return 结果
     */
    @Override
    public int deleteDcGoodsById(String id)
    {
        return dcGoodsMapper.deleteDcGoodsById(id);
    }
}
